package com.tedu.day07springmvc.controller;

import com.tedu.day07springmvc.mapper.UserMapper;
import com.tedu.day07springmvc.pojo.dto.UserDTO;
import com.tedu.day07springmvc.pojo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
public class UserController {
    @Autowired
    private UserMapper userMapper;
    //第一种方法
/*    @RequestMapping("/regUser")
    @ResponseBody
    public String regUser(HttpServletRequest request){
        System.out.println("开始处理用户注册");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");
        int age = Integer.parseInt(ageStr);
        System.out.println(username+","+password+","+nickname+","+age);
        return "注册成功";
    }*/
    //第二种方法
/*@RequestMapping("/regUser")
@ResponseBody
public String regUser(String username,String password,String nickname,String age){
    System.out.println("注册信息:"+username+","+password+","+nickname+","+age);
    return "注册成功";
}*/
@RequestMapping("/regUser")
@ResponseBody
public String regUser(UserDTO userDTO){
    System.out.println(userDTO);
    //实例化User,表示即将插入到User表中的一条记录
    User user = new User();
    //将用户在注册时提交的数据封装到user对象中
    user.setUsername(userDTO.getUsername());
    user.setPassword(userDTO.getPassword());
    user.setNickname(userDTO.getNickname());
    user.setAge(userDTO.getAge());
    user.setCreated(new Date());
    int num = userMapper.insertUser(user);
    return num>0?"注册成功":"注册失败";
    }
}
